﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuickGame.Model;
using DevExpress.Web.ASPxGridView;

namespace QuickGame.View
{
    public partial class QLNguoiDung : System.Web.UI.Page
    {
        private ModelContext modelContext;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.modelContext = new ModelContext();
            if (Session["idnguoidung"] == null)
            {
                Response.Redirect("/Account/Login.aspx");
            }
            else
            {
                long idnguoidung = (long)Session["idnguoidung"];
                var nguoidung = NguoiDungHs.QueryEntity(modelContext, idnguoidung);
                if (nguoidung.IDQuyen == 1)
                {
                    this.LoadDL();
                }
                else
                {
                    Response.Redirect("/Account/Login.aspx");
                }
            }
        }
        public void LoadDL()
        {
            this.modelContext = new ModelContext();
            var ds = NguoiDungHs.QueryEntities(modelContext).Select(k => new NguoiDungView
            {
                ID = k.ID,
                Ma = k.Ma,
                HoTen = k.Ten,
                NgaySinh = k.NgaySinh,
                GioiTinh = k.GioiTinh,
                Email = k.Email,
                TenDangNhap = k.TenDangNhap
            });
            Session["DSNguoiDung"] = ds;
            
            grvNguoiDung.DataSource = ds;
            grvNguoiDung.DataBind();
        }

        protected void GridView_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            ASPxGridView grid = sender as ASPxGridView;
            var id = e.Keys[0];
            NguoiDungHs.Delete(modelContext, (long)id);
            if (this.modelContext.SaveChanges() > 0)
            {
                this.LoadDL();
            }
            e.Cancel = true;
        }

        //protected void GridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        //{
        //    ASPxGridView grid = sender as ASPxGridView;
        //    var nguoidung = NguoiDungHs.Insert(modelContext);
        //    nguoidung.Ma = (string)e.NewValues["Ma"];
        //    nguoidung.Ten = (string)e.NewValues["HoTen"];
        //    nguoidung.NgaySinh = (DateTime?)e.NewValues["NgaySinh"];
        //    nguoidung.GioiTinh = (bool?)e.NewValues["GioiTinh"];
        //    nguoidung.Email = (string)e.NewValues["Email"];
        //    if (this.modelContext.SaveChanges() > 0)
        //    {
        //        this.LoadDL();
        //    }
        //    e.Cancel = true;
        //    grvNguoiDung.CancelEdit();
        //}

        protected void GridView_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            long id = (long)e.Keys[0];
            NguoiDungView view = new NguoiDungView();
            view.ID = id;
            view.Ma = (string)e.NewValues["Ma"];
            view.HoTen = (string)e.NewValues["HoTen"];
            view.NgaySinh = (DateTime?)e.NewValues["NgaySinh"];
            view.GioiTinh = (bool?)e.NewValues["GioiTinh"];
            view.Email = (string)e.NewValues["Email"];
            view.TenDangNhap = (string)e.NewValues["TenDangNhap"];
            Session["NguoiDungView"] = view;
            NguoiDungHs.Update(modelContext, view);
            this.LoadDL();
            e.Cancel = true;
            grvNguoiDung.CancelEdit();
        }

        protected void GridView_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {

        }
    }
}